﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 注册登录
{
    public partial class RegisterWindow : Form
    {
        LoginWindow loginWindow;
        public RegisterWindow(LoginWindow loginWindow)
        {
            InitializeComponent();
            this.loginWindow = loginWindow;
        }

        private void backButton_Click(object sender, EventArgs e)
        {
            this.loginWindow.Show();
            this.Close();
        }

        private void checkNameLabel_Click(object sender, EventArgs e)
        {


            if (checkNameExistence())
            {
                MessageBox.Show("用户名已存在，无法注册");
                return;
            }
            else
            {
                MessageBox.Show("用户名不重复，您可以注册");
                return;
            }
        }

        private void regButton_Click(object sender, EventArgs e)
        {
            String nickName = nickNameTextBox.Text;
            String psw = pswTextBox.Text;
            String realName = realNameTextBox.Text;
            String birth = dateTimePicker1.Text;
            String email = emailTextBox.Text;

            //检验必填项是否都填写
            if (nickName.Length == 0 || psw.Length == 0)
            {
                MessageBox.Show("带星号的信息是必须填写的");
                return;
            }
            else if (psw != pswTextBox2.Text)
            {
                //检测两次输入密码是否一致
                MessageBox.Show("两次输入的密码不一样");
                return;
            }
            if (checkNameExistence())
            {
                MessageBox.Show("用户名已存在，注册失败");
                return;
            }
            string sql = "insert into users values(@username,@userpsw,@cn,@birth,@email)";
            SqlParameter[] parameters = new SqlParameter[]
            {
                    new SqlParameter("@username",nickName),
                    new SqlParameter("@userpsw",psw),
                    new SqlParameter("@cn",realName),
                    new SqlParameter("@birth",birth),
                    new SqlParameter("@email",email)
            };
            if (Connection.SqlNoQuery(sql, parameters))
            {
                MessageBox.Show("注册成功");
            }
            else
            {
                MessageBox.Show("SQL执行失败");
            }
        }
        private bool checkNameExistence()
        {
            // 返回值若为真，则说明该用户存在
            String nickName = nickNameTextBox.Text;
            String sql = "SELECT userName FROM users where userName=@name";
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@name",nickName)
            };
            DataSet data = Connection.SqlQuery(sql, parameters);

            if (data.Tables["Info"].Rows.Count < 1)
            {
                return false;//用户名不存在
            }
            else
            {
                return true;
            }
        }
    }
}
